//应收账单管理/收入明细
//杨帆

interface IReceivableBillIncomeManagentViewModel extends ng.IScope{
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<IncomeListItemResponse>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 结算单位
     */
    settleName:string;
    /**
     * 收入总额
     */
    incomeMoney:number;
    /**
     * 总额
     */
    totalMoney:number;
    /**
     * 不含税
     */
    excludingTax:number;
    /**
     * 新增收入
     */
    addIncome:()=>void;
    /**
     * 收入明细数据
     */
    incomeAddData:IncomeListItemResponse;
    /**
     * 删除收入
     */
    deleteIncome:(id:string)=>void;
    /**
     * 列表高
     */
    heightA:number;

    back:()=>void;
}

class IReceivableBillIncomeManagentController {
    gridApi: uiGrid.IGridApiOf<ReceivableSettleBillListItemResponse>;
    settleName:string;
    settleId:string;
    
    constructor(private $scope:IReceivableBillIncomeManagentViewModel,private receivableService:IReceivableService,private $location:ng.ILocationService,
    private $state: angular.ui.IStateService){
        this.init();        
    }

    init():void{
        this.$scope.settleName = this.$location.search().name;
        this.settleId = this.$location.search().id;

        $('#openshipStartTime').click( ()=> {
        $('#shipStartTime').datetimepicker('show');
    });

        this.$scope.gridOptions = {
            enableSelectAll: false,//全选禁用
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: false,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs:[
                { displayName:'收入编号',field:'incomeCode',enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: "结算单位", field: 'settle', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: "收入金额", field: 'amount', enableColumnMenu: false, cellTooltip:true, headerTooltip:true },
                { displayName: '收入说明', field: 'description', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: '收款日期', field: 'receiptTime', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { 
                    displayName: "操作",
                        field: 'orderStatus',
                        cellTemplate: '<a class="primary fa fa-trash-o text-danger m-l-xs" title="删除" ng-click="grid.appScope.deleteIncome(row.entity.id)"></a>',
                        enableColumnMenu: false, 
                        enableSorting: false
                }
            ],
        };

        this.load();
        this.$scope.addIncome = this.addIncome;
        this.$scope.deleteIncome = this.delIncome;
        this.$scope.back = this.back;

    }

    /**
     * 加载数据
     */
    load=()=>{
        this.receivableService.incomeList(this.$scope.settleName,"","").then((res)=>{
            this.$scope.gridOptions.data = res.data.data ;
            this.$scope.heightA = (10 * 39) + 95;
            /**
             * 获取收入总额
             */
            let amount = [];

            res.data.data.forEach((item,index)=>{
                amount.push(item.amount);
               
            });
            this.$scope.incomeMoney = eval(amount.join('+')) || 0;
            /**
             * 获取开票总额
             */
            this.receivableService.getreceivableSettleBillList("",null,"","",this.$scope.settleName,"","", "", "", "", "","","",this.$scope.settleName, 0, -1).then((result)=>{
                let arr=result.data.data;
                let totalM = [];
                let excludingT=[];
                arr.forEach((item,index)=>{
                    if(item.invoiceStatus=="已开票"){
                        totalM.push(item.totalMoney);
                    }else if(item.invoiceStatus=="不含税"){
                        excludingT.push(item.totalMoney);
                    }
                });
                this.$scope.totalMoney = eval(totalM.join('+')) || 0;
                this.$scope.excludingTax = eval(excludingT.join('+')) || 0;
            });
        });
    }

    /**
     * 断言
     */
    isReceivableListResponse(response: string | IncomeListItemResponse[]): response is IncomeListItemResponse[] {
        return (<IncomeListItemResponse[]>response).forEach !== undefined;
    }

    /**
     * 增加收入
     */
    addIncome = ()=> {
        this.receivableService.income(
            this.$scope.incomeAddData.amount,
            this.$scope.incomeAddData.description,
            this.settleId,
            this.$scope.settleName,
            this.$scope.incomeAddData.receiptTime
        ).then((res)=>{
            this.load();
        });
    }

    /**
     * 删除收入
     */
    delIncome = (id:string)=> {
        this.receivableService.deleteincome(id).then((res)=>{
            this.load();
        });
    }

    //
    back=()=>{
        this.$state.go("app.receivable.receivableBill",{name:'receivableBillIncome'})
    }

}

angular.module("tms").controller("receivableBillIncomeManagentController", ["$scope", "receivableService", "$location","$state" , IReceivableBillIncomeManagentController]);